﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SetHotkey;

namespace PText_Tool
{
    public partial class PTtool : Form
    {

        private const int WM_HOTKEY = 0x312; //窗口消息-热键
        private const int WM_CREATE = 0x1; //窗口消息-创建
        private const int WM_DESTROY = 0x2; //窗口消息-销毁
        private const int Space = 0x3572; //热键ID
        private const int Space2 = 0x5572; //热键ID
        private const int Space3 = 0x6572; //热键ID
        private const int Space4 = 0x7572; //热键ID
        private const int Space5 = 0x8572; //热键ID
        private const int Space6 = 0x9572; //热键ID
        private const int Space7 = 0x0572; //热键ID
        private const int Space8 = 0x4172; //热键ID
        private const int Space9 = 0x4272; //热键ID
        private const int Space10 = 0x3372; //热键ID
        private const int Space11 = 0x3472; //热键ID
        private const int Space12 = 0x4572; //热键ID

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case WM_HOTKEY: //窗口消息-热键ID
                    switch (m.WParam.ToInt32())
                    {
                        case Space: //热键ID
                            Clipboard.SetDataObject(textBox1.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space2:
                            Clipboard.SetDataObject(textBox2.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space3:
                            Clipboard.SetDataObject(textBox3.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space4:
                            Clipboard.SetDataObject(textBox4.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space5:
                            Clipboard.SetDataObject(textBox5.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space6:
                            Clipboard.SetDataObject(textBox6.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space7:
                            Clipboard.SetDataObject(textBox7.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space8:
                            Clipboard.SetDataObject(textBox8.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space9:
                            Clipboard.SetDataObject(textBox9.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space10:
                            Clipboard.SetDataObject(textBox10.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space11:
                            Clipboard.SetDataObject(textBox11.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        case Space12:
                            Clipboard.SetDataObject(textBox12.Text);
                            System.Windows.Forms.SendKeys.Send("^v");
                            break;
                        default:
                            break;
                    }
                    break;
                case WM_CREATE: //窗口消息-创建
                    Hotkeys.AppHotKey.RegKey(Handle, Space, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F1);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space2, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F2);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space3, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F3);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space4, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F4);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space5, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F5);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space6, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F6);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space7, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F7);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space8, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F8);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space9, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F9);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space10, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F10);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space11, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F11);  //热键
                    Hotkeys.AppHotKey.RegKey(Handle, Space12, Hotkeys.AppHotKey.KeyModifiers.Ctrl, Keys.F12);  //热键

                    break;
                case WM_DESTROY: //窗口消息-销毁
                    Hotkeys.AppHotKey.UnRegKey(Handle, Space); //销毁热键
                    break;
                default:
                    break;
            }
        }

        string configPath = @"C:\Users\jinke\Documents\PText_Tool\config.dat";
        public PTtool()
        {
            InitializeComponent();
        }

        private void PTtool_Load(object sender, EventArgs e)
        {
            string[] text10 = new string[12];

            if (!Directory.Exists(@"C:\Users\jinke\Documents\PText_Tool\"))
            {
                Directory.CreateDirectory(@"C:\Users\jinke\Documents\PText_Tool\");
            }

            if (!File.Exists(configPath))
            {
                FileStream fs = new FileStream(configPath, FileMode.Create, FileAccess.ReadWrite);
                fs.Close();
                string[] text = { "", "", "", "", "", "", "", "", "", "", "", "" };
                File.WriteAllLines(configPath, text);
            }


            try //reading error
            {
                text10 = File.ReadAllLines(configPath);
            }
            catch (Exception)
            {
                MessageBox.Show("无法从数据文件中读取数据。", "reading error");
            }

            try //error 47
            {
                textBox1.Text = text10[0];
                textBox2.Text = text10[1];
                textBox3.Text = text10[2];
                textBox4.Text = text10[3];
                textBox5.Text = text10[4];
                textBox6.Text = text10[5];
                textBox7.Text = text10[6];
                textBox8.Text = text10[7];
                textBox9.Text = text10[8];
                textBox10.Text = text10[9];
                textBox11.Text = text10[10];
                textBox12.Text = text10[11];
            }
            catch (Exception)
            {
                MessageBox.Show("error 47", "error");
            }
        }

        private void saveTextbutton_Click(object sender, EventArgs e)
        {
            string[] text10 = { textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text, textBox10.Text, textBox11.Text, textBox12.Text };
            File.WriteAllLines(configPath, text10);
            MessageBox.Show("已保存");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            notifyIcon1.Visible = true;
            this.Visible = false;
        }

        private void 显示程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            notifyIcon1.Visible = false;
            this.Visible = true;
        }

        private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            textBox12.Text = "";
        }
    }
}
